package cn.chenxiejia;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import static java.awt.Font.createFont;

/***
 * @program: maven
 * @ClassName: Imges5
 * @Description: 加载字体
 * @Author: chenxiejia
 * @Date: 2022/1/22 21:05
 * @version : V1.0
 */
public class Imges5 {
    public static void main(String[] args) throws IOException {
        BufferedImage i = new BufferedImage(400, 400, 2);
        var m = i.createGraphics();
        m.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        m.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        m.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        m.setColor(Color.YELLOW);
        m.fillRect(0, 0, 400, 400);
        String s = "Hello world 版权所有，陈卸甲！";
        Font font = new Font("Aa狂派手书", Font.BOLD, 20);
        //反射机制获取外部字体
        var is = Imges5.class.getClassLoader().getResourceAsStream("font/f1.ttf");
        try {
            font = createFont(Font.TRUETYPE_FONT, is);

        } catch (FontFormatException e) {
            e.printStackTrace();
        }
        //获取外部字体方式2
//        var is2 = Thread.currentThread().getContextClassLoader().getResourceAsStream("font/f1.ttf");
//        try {
//            font=Font.createFont(0,is2);
//        } catch (FontFormatException e) {
//            e.printStackTrace();
//        }
        System.out.println(font.getName());
        var fm = m.getFontMetrics(font);
        m.setColor(Color.red);
        font = font.deriveFont(50f);
        m.setFont(font);
        m.drawString(s, 10, 50);
        m.dispose();
        ImageIO.write(i, "png", new File("ziti.png"));
        Runtime.getRuntime().exec("cmd /k start ziti.png");

    }
}
